﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;

using EdgeStatz.ESSystems.Objects;

namespace EdgeStatz.ModelEngine.DatabaseTools
{
    class DEXMLWriter
    {
        private string path;
        private List<TableData> tableData = new List<TableData> { };

        public TableData[] TableData
        {
            get { return tableData.ToArray(); }
            set { tableData.AddRange(value); }
        }

        public DEXMLWriter(string filePath)
        {
            path = filePath;
        }

        public void AddTable(TableData data)
        {
            tableData.Add(data);
        }

        public bool TableExists(string name)
        {
            foreach (TableData tdata in tableData)
                if (tdata.Name.Trim().ToLower().Equals(name.Trim().ToLower()))
                    return true;
            return false;
        }

        public void Write()
        {
            XmlDocument myXmlDocument = new XmlDocument();
            XmlNode node = myXmlDocument.CreateElement("Data");

            foreach (TableData tdata in tableData)
            {
                XmlNode node_table = myXmlDocument.CreateElement("Table");
                XmlAttribute nameAtr = myXmlDocument.CreateAttribute("Name");
                nameAtr.Value = tdata.Name;
                node_table.Attributes.Append(nameAtr);

                for (int i = 0; i < tdata.ColumnCount; i++)
                {
                    XmlNode node_col = myXmlDocument.CreateElement("Column");
                    XmlAttribute nameAtr_col = myXmlDocument.CreateAttribute("Name");
                    XmlAttribute typeAtr_col = myXmlDocument.CreateAttribute("Type");
                    nameAtr_col.Value = tdata.ColumnNames[i];
                    typeAtr_col.Value = tdata.ColumnTypes[i].ToString();

                    node_col.Attributes.Append(nameAtr_col);
                    node_col.Attributes.Append(typeAtr_col);

                    node_table.AppendChild(node_col);
                }
                foreach (RowData rdata in tdata.Rows)
                {
                    XmlNode node_datarow = myXmlDocument.CreateElement("DataRow");
                    for (int j = 0; j < rdata.Count; j++)
                    {
                        XmlNode node_val = myXmlDocument.CreateElement("Value");
                        if (!rdata.Items[j].GetType().Equals(typeof(DBNull)))
                            node_val.InnerText = rdata.Items[j].ToString();
                        else
                        {
                            XmlAttribute nullAtr_val = myXmlDocument.CreateAttribute("Null");
                            nullAtr_val.Value = "true";
                            node_val.Attributes.Append(nullAtr_val);
                        }
                        node_datarow.AppendChild(node_val);
                    }
                    node_table.AppendChild(node_datarow);
                }
                node.AppendChild(node_table);
            }

            myXmlDocument.AppendChild(node);
            myXmlDocument.Save(path);
        }
    }
}
